//
//  TopicTableViewCell.m
//  NewChat
//
//  Created by lanou3g on 16/2/27.
//  Copyright © 2016年 蓝鸥. All rights reserved.
//

#import "TopicTableViewCell.h"



//定义宏，用来cell布局
#define kWidth self.contentView.frame.size.width
#define kHeight self.contentView.frame.size.height

@implementation TopicTableViewCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.backgroundColor = [UIColor whiteColor];
        [self addAllViews];
        
    }
    
    return self;
    
}

#pragma mark---------添加子控件-------------
-(void)addAllViews{
    
    [self addSubview:self.userPictureBtn];
    [self addSubview:self.nickNameLbl];
    [self addSubview:self.timeLbl];
    [self addSubview:self.topicLbl];
    [self addSubview:self.messageBtn];
    [self addSubview:self.voiceBtn];
    
}

#pragma mark----------根据父视图自适应------
/**
 *  自定义的方法，cell根据lable自适应（使用这个方法不需要重写layoutSubviews方法）
 *
 *  @param model cell中子控件需要的数据
 */

/*
 -(void)configureCellWithModel:(TopicModel *)model{
 
 _userPictureBtn.frame = CGRectMake(kWidth*0.05, kHeight*0.1, kHeight*0.3, kHeight*0.3);
 //设置圆角
 _userPictureBtn.layer.cornerRadius = kHeight*0.15;
 _userPictureBtn.layer.masksToBounds = YES;
 
 _userPictureBtn.layer.cornerRadius = kHeight*0.15;
 _userPictureBtn.layer.masksToBounds = YES;
 _nickNameLbl.frame = CGRectMake(kWidth*0.22, kHeight*0.1, kWidth*0.3, kHeight*0.15);
 _timeLbl.frame = CGRectMake(kWidth*0.22, kHeight*0.27, kWidth*0.3, kHeight*0.15);
 
 _topicLbl.text = model.topicTitle;
 CGSize size = [self.topicLbl sizeThatFits:CGSizeMake(kWidth*0.9, MAXFLOAT)];
 _topicLbl.frame = CGRectMake(kWidth*0.05, kHeight*0.5, kWidth*0.9, size.height);
 
 _messageBtn.frame = CGRectMake(kWidth*0.05, kHeight*0.75, kWidth*0.5, kHeight*0.2);
 //设置圆角
 _messageBtn.layer.cornerRadius = kHeight*0.1;
 _messageBtn.layer.masksToBounds = YES;
 //设置边框
 _messageBtn.layer.borderWidth = 1;
 
 self.frame = CGRectMake(0, 0, kWidth, CGRectGetMaxY(self.topicLbl.frame)+CGRectGetHeight(self.messageBtn.frame)+50);
 
 }
 */


#pragma mark----------根据父视图自适应------
-(void)layoutSubviews{
    [super layoutSubviews];
    
    _userPictureBtn.frame = CGRectMake(kWidth*0.05, kHeight*0.1, kHeight*0.3, kHeight*0.3);
    _userPictureBtn.layer.cornerRadius = kHeight*0.15;
    _userPictureBtn.layer.masksToBounds = YES;
    _nickNameLbl.frame = CGRectMake(kWidth*0.22, kHeight*0.1, kWidth*0.3, kHeight*0.15);
    _timeLbl.frame = CGRectMake(kWidth*0.22, kHeight*0.27, kWidth*0.3, kHeight*0.15);
    _topicLbl.frame = CGRectMake(kWidth*0.05, kHeight*0.45, kWidth*0.9, kHeight*0.25);
    _messageBtn.frame = CGRectMake(kWidth*0.75, kHeight*0.1, kWidth*0.2, kHeight*0.15);
    _voiceBtn.frame = CGRectMake(kWidth*0.05, kHeight*0.75, kWidth*0.55, kHeight*0.2);

    
}


#pragma mark----------懒加载子控件----------

-(UIButton *)userPictureBtn{
    
    if (!_userPictureBtn) {
        
        _userPictureBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _userPictureBtn.frame = CGRectMake(kWidth, kHeight, kWidth, kHeight);
        _userPictureBtn.backgroundColor = [UIColor clearColor];
        [_userPictureBtn addTarget:self action:@selector(clickUserPictureBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _userPictureBtn;
}


-(void)clickUserPictureBtnAction:(UIButton*)sender{
    
    NSLog(@"点击了TopicTableViewController中cell上的子控件UserPictureBtn,代理会去执行跳转方法");
    [self.delegate noticeTopicViewPushNextView];
  
}

-(UILabel *)nickNameLbl{
    if (!_nickNameLbl) {
        
        _nickNameLbl = [[UILabel alloc]init];
        _nickNameLbl.frame = CGRectMake(kWidth, kHeight, kWidth, kHeight);
        _nickNameLbl.backgroundColor = [UIColor whiteColor];
    }
    return _nickNameLbl;
    
}

-(UILabel *)timeLbl{
    if (!_timeLbl) {
        _timeLbl = [[UILabel alloc]init];
        _timeLbl.frame = CGRectMake(kWidth, kHeight, kWidth, kHeight);
        _timeLbl.backgroundColor = [UIColor whiteColor];
    }
    return _timeLbl;
    
}

-(UILabel *)topicLbl{
    if (!_topicLbl) {
        _topicLbl = [[UILabel alloc]init];
        _topicLbl.frame = CGRectMake(kWidth, kHeight, kWidth, kHeight);
        _topicLbl.backgroundColor =[UIColor whiteColor];
    }
    return _topicLbl;
}

-(UIButton *)messageBtn{
    if (!_messageBtn) {
        _messageBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _messageBtn.frame = CGRectMake(kWidth, kHeight, kWidth, kHeight);
        _messageBtn.backgroundColor = [UIColor whiteColor];
        [_messageBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
        
    }
    return _messageBtn;
    
}

-(UIButton *)voiceBtn{
    if (!_voiceBtn) {
        _voiceBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        _voiceBtn.frame = CGRectMake(kWidth, kHeight, kWidth, kHeight);
        _voiceBtn.backgroundColor = [UIColor cyanColor];
        _voiceBtn.layer.cornerRadius = 20;
        [_voiceBtn addTarget:self action:@selector(clickVoiceBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _voiceBtn;
}

-(void)clickVoiceBtnAction:(UIButton*)sender{
    
    NSLog(@"点击了TopicTableViewCell的子控件——>VoiceBtn");
    NSLog(@"这个控件的点击事件在TopicTableViewCell.m中");
    NSLog(@"还没有写播放语音的功能");
    
}


- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
